package com.jmx.yp.service.role;

import java.util.*;

import com.jmx.yp.dao.IBaseDao;
import com.jmx.yp.entity.Role;
import com.jmx.yp.util.Tools;

/**
 * 
 * 功能描述：
 *			IRoleService接口实现类
 * @author Jenny
 *
 * Date:2011-1-22 下午10:17:03
 */
public class RoleService implements IRoleService {
	
	private IBaseDao dao;
	
	public void setDao(IBaseDao dao){
		this.dao = dao;
	}

	/**
	 * 根据角色类型及组织编号查询角色列表
	 * @param roleType
	 * @param groupId
	 * @param flag true标志是杰马逊员工
	 * @return 返回LIST是MAP对象（其中只有角色ID及角色名称）
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List getEmployeeRole(Integer roleType, Long groupId, boolean flag) throws Exception{
		if(flag){
			List list = dao.getList("com.jmx.yp.entity.RoleMapper.getEmployeeJMXRoleList", null);
			return list;
		}else{
			Map map = new HashMap(2);
			map.put("role_type", roleType);
			map.put("group_srl", groupId);
			List list = dao.getList("com.jmx.yp.entity.RoleMapper.getEmployeeRoleList", map);
			
			return list;
		}
	}
	
	/**
	 * 根据角色名称查询角色列表
	 * @param groupId 若为空，则表示查询杰马逊角色信息
	 * @param roleName
	 * @param nowPage 当前页
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map getRoleList(Long groupId, String roleName, int nowPage) throws Exception{
		Map map = new HashMap(2);
		int pageSize = dao.getPageSize();
		
		//参数MAP
		Map paramMap = new HashMap();
		
		if(roleName != null && roleName.trim().length() > 0)
			paramMap.put("roleName", "%" + roleName.trim() + "%");
		
		if(groupId != null){
			paramMap.put("groupId", groupId);
			paramMap.put("roleType", Role.ROLE_TYPE_GROUP);
		}
		
		//总数
		int total = (Integer)dao.getObject("com.jmx.yp.entity.RoleMapper.countRoleList", paramMap);
		
		paramMap.put("pageSize", pageSize);
		paramMap.put("page", (nowPage - 1) * pageSize);
		
		//LIST
		List list = dao.getList("com.jmx.yp.entity.RoleMapper.getRoleList", paramMap);
		if(nowPage > 1 && (list == null || list.isEmpty())){ //当前页>1，查询又无结果，说明是以前的页数已经被删完。则自动显示最后一页
			nowPage = Tools.computeTotalPage(total, pageSize);
			paramMap.put("page", (nowPage - 1) * pageSize);
			list = dao.getList("com.jmx.yp.entity.RoleMapper.getRoleList", paramMap);
		}
		
		//handleListTel(list);
		
		map.put("total", total); //总记录数
		map.put("rows", list);
		map.put("nowPage", nowPage); //当前页
		map.put("pageTotal", Tools.computeTotalPage(total, pageSize));                    //总页数
		
		return map;
	}
}
